package com.avic.rong.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.api.SendCodeApi;
import com.avic.rong.api.mine.UpPwdApi;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.TimeCount;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.StringUtil;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//账号设置-个人信息-修改密码x
public class UpPwdActivity extends BaseActivity {

    @BindView(R.id.edittext_newpwd)
    EditText edittext_newpwd;
    @BindView(R.id.edittext_newcode)
    EditText edittext_newcode;
    @BindView(R.id.bt_send_newcode)
    Button bt_send_newcode;
    @BindView(R.id.btn_okup)
    Button btn_okup;
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    private String mobile;
    private SendCodeApi sendCodeApi;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_up_pwd;
    }

    @Override
    protected void initView() {
        mobile = getIntent().getStringExtra("mobile");
        String substring = mobile.substring(3, 7);
        String replace = mobile.replace(substring, "****");
        tv_phone.setText(replace);
        tvTitle.setText("修改密码");


    }

    @Override
    protected void addListener() {

    }

    public static Intent queryIntent(Context context) {
        Intent intent = new Intent(context, UpPwdActivity.class);
        return intent;
    }

    @OnClick({ R.id.bt_send_newcode, R.id.btn_okup})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_send_newcode:
                String pwd = edittext_newpwd.getText().toString().trim();
                if (StringUtil.isEmpty(pwd)){
                    Toast toast = Toast.makeText(context, "密码不能为空", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER,0,0);
                    toast.show();
                }else {
                    sendCode();
                }
                break;
            case R.id.btn_okup:
                upPwd();
                break;
        }
    }

    private void upPwd() {

        String code = edittext_newcode.getText().toString().trim();
        String newPwd = edittext_newpwd.getText().toString().trim();

        String login_token = SharedPreferencesUtil.getLoginToken();

        UpPwdApi upPwdApi = new UpPwdApi(this,upPwdListener );
        upPwdApi.setArgument(mobile,code,newPwd,login_token);

        HttpManager.getInstance().doHttpDeal(upPwdApi);
    }

    HttpOnNextListener<BaseResultEntity> upPwdListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)){
                ToastTools.showPublicToast("修改成功");
                    finish();
            }else {
                Toast toast = Toast.makeText(UpPwdActivity.this, baseResultEntity.getMsg(), Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }
    };


    private void sendCode() {
        sendCodeApi = new SendCodeApi(this, sendNewCodeListener);


        sendCodeApi.setArguments(mobile, 9, SharedPreferencesUtil.SIGN);
        HttpManager.getInstance().doHttpDeal(sendCodeApi);
    }
    HttpOnNextListener<BaseResultEntity> sendNewCodeListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                timeOut(bt_send_newcode);
            }else {
                Toast toast = Toast.makeText(UpPwdActivity.this, baseResultEntity.getMsg(), Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }
    };


    private void timeOut(TextView tv) {
        TimeCount timeCount = new TimeCount(60000, 1000, tv,true);
        timeCount.start();
    }

}
